// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import MonoSingle from "./MonoSingle";

const { ccclass, property, menu} = cc._decorator;

@ccclass
@menu("Utility/ScrollViewEvent")
export default class ScrollViewEvent extends cc.Component {

	@property(cc.Button)
	scrollItemList: Array<[cc.Button]> = new Array<[cc.Button]>();
	@property
	scrollArray: number = 4
	@property
	scrollWidth: number = 400

	@property
	startPos :number = 0;

	@property
	endPos: number = 0;

	@property
	isVer: boolean = true

    // onEnable () {


			// var self = this
			// var _scrollViewEvent = self.node.getComponentInChildren(ScrollViewEvent);
			// var func = function () {
			// 	for (let i = 0; i < self.node.getComponentsInChildren(cc.Button).length; i++) {
			// 		let vaule = self.node.getComponentsInChildren(cc.Button)[i];
			// 		_scrollViewEvent.pushItemList(vaule);
			// 	}
			// 	MonoSingle.GetTvBoxInput().pushSavePrePanelTop(self.node)
			// 	MonoSingle.GetTvBoxInput().initTvBoxCurPanelEvent(0, self.node)
			// }
			// MonoSingle.GetTvBoxInput().scheduleOnceEvent(0.2, func)


			// MonoSingle.GetTvBoxInput().addCurBtnChangeAction(this.name + "curBtnChangeEvent", function (curBtn) {
			// 	_scrollViewEvent.updatePos(curBtn)
			// });

    // }
	// onDisable()
	// {
	// 	MonoSingle.GetTvBoxInput().removeCurBtnChangeAction(this.name + "curBtnChangeEvent");
	// }
	pushItemList(btns)
	{
		if (Object.prototype.toString.call(btns).slice(8, -1)=="Array")
		{
			this.scrollItemList.push(btns)
		}
		else
		{
			this.scrollItemList.push([btns])
		}
		
		
	}
	reset()
	{
		this.scrollItemList = new Array<[cc.Button]>();
	}

	updateScrollView(_curBtn)
	{
		for (let i = 0; i < this.scrollItemList.length; i++) {
			let _array = this.scrollItemList[i];
			var index = _array.indexOf(_curBtn)
			if (index != -1) {
				if (this.isVer)
					this.node.setPosition(this.node.position.x, Math.floor(i / this.scrollArray) * this.scrollWidth)
				else
					this.node.setPosition(-Math.floor(i / this.scrollArray) * this.scrollWidth, this.node.position.y)
			}
		}


	}

	updatePos(_curBtn) {
		for (let i = 0; i < this.scrollItemList.length; i++) {
			let _array = this.scrollItemList[i];
			var index = _array.indexOf(_curBtn)
			if (index != -1) {
				if (this.isVer) {
					var _y =  MonoSingle.GetTvBoxInput().getPos(_curBtn.node).y;
					var _x = MonoSingle.GetTvBoxInput().getPos(_curBtn.node).x
					cc.error(_x,_y);
					if (_y < this.endPos) {
						
						this.node.setPosition(this.node.position.x, this.node.position.y + (this.startPos - _y))
					}
					else if (_y > this.startPos) {
						cc.error(this.node.position.y, this.endPos,_y)
						this.node.setPosition(this.node.position.x, this.node.position.y - _y + this.endPos)
					}
				}
			}
		}
	}

}
